package com.isoftstone.project.timedtask.service;

import com.isoftstone.project.facilities.management.domain.FacilitiesManagement;
import com.isoftstone.project.facilities.management.mapper.FacilitiesManagementMapper;
import com.isoftstone.project.facilities.underdata.domain.MonitorUnderParameter;
import com.isoftstone.project.facilities.underdata.mapper.MonitorUnderParameterMapper;
import com.isoftstone.util.ParamConfig;
import com.isoftstone.util.SerialPortUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.List;

@Service
@Slf4j
public class RunTask {

    private static RunTask ourInstance = null;
    public synchronized static RunTask getInstance() {
        if(null == ourInstance){
            ourInstance = new RunTask();
        }
        return ourInstance;
    }
    public RunTask(){}
    @Autowired
    MonitorUnderParameterMapper monitorUnderParameterMapper;
    @Autowired
    FacilitiesManagementMapper facilitiesManagementMapper;
    public void run(){
        log.info("**********************井下获取参数开始***********************");
        SerialPortUtils serialPort = new SerialPortUtils();
        // 创建串口必要参数接收类并赋值，赋值串口号，波特率，校验位，数据位，停止位
       MonitorUnderParameter monitorUnderParameter= monitorUnderParameterMapper.selectall();//查询井下串口参数，端口号和串口名
        log.info("串口号**********************"+monitorUnderParameter.getContact());
        log.info("波特率**********************"+monitorUnderParameter.getUdpport());
        log.info("校验位**********************"+"0");
        log.info("数据位**********************"+"8");
        log.info("停止位**********************"+"1");
        ParamConfig paramConfig = new ParamConfig(monitorUnderParameter.getContact(), Integer.valueOf(monitorUnderParameter.getUdpport()), 0, 8, 1);
        // 初始化设置,打开串口，开始监听读取串口数据
        serialPort.init(paramConfig);
      List<FacilitiesManagement> facilitiesManagementList= facilitiesManagementMapper.selectall();//查所有，循环发送

        for (FacilitiesManagement facilitiesManagement : facilitiesManagementList) {
            log.info("设备号**********************"+facilitiesManagement.getManagementId());
            log.info("设备名称********************"+facilitiesManagement.getManagementName());
            log.info("功能码**********************"+facilitiesManagement.getFunctionCode());
            log.info("数据起始位1*****************"+facilitiesManagement.getStartBit1());
            log.info("数据起始位2*****************"+facilitiesManagement.getStartBit2());
            log.info("数据个数1*******************"+facilitiesManagement.getDataBit1());
            log.info("数据个数2*******************"+facilitiesManagement.getDataBit2());
            log.info("安装位置********************"+facilitiesManagement.getManagementLocation());
            String data=facilitiesManagement.getManagementId()+facilitiesManagement.getFunctionCode()+facilitiesManagement.getStartBit1()+facilitiesManagement.getStartBit2()+facilitiesManagement.getDataBit1()+facilitiesManagement.getDataBit2();//拼装数据
            String crcdata= SerialPortUtils.getCRCModBus(data.getBytes());//CRC算法，由高到低
            String msg=data+crcdata;
            log.info("数据********************"+data);
            log.info("CRC算法********************"+crcdata);
            log.info("发送数据********************"+msg);
            // 调用串口操作类的sendComm方法发送数据到串口
            serialPort.sendComm(msg);

            //01030016001625C0
            //01032C3D88F1F5000000000000000041200000000000004100000000000047C3500041B4D5E23B03126F3F8000004C17
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            serialPort.closeSerialPort();
        }

    }

}
